using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Trans.Refunds;
using JlPay.Sdk.Models.Trans.Refunds;

namespace JlPay.Sdk.Examples.Trans.Refunds
{
    /// <summary>
    /// 交易 - 退款查询功能使用示例
    /// </summary>
    public class RefundsQueryDemo
    {
        /// <summary>
        /// 构建退款查询请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static RefundsQueryRequest BuildRefundsQueryRequest()
        {
            var request = new RefundsQueryRequest();
            
            // 设置请求参数
            request.MchId = "849581183510017";
            // request.TransactionId = "451147688260420350771289";
            request.OutTradeNo = "0c12656513f74a8f";
            
            return request;
        }

        /// <summary>
        /// 退款查询示例
        /// </summary>
        public static async Task RefundsQueryDemoAsync()
        {
            Console.WriteLine("开始执行退款查询示例");

            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            var refundsQueryService = new RefundsQueryService(config);

            // 创建退款查询请求
            var request = BuildRefundsQueryRequest();

            try
            {
                // 发送请求
                var response = await refundsQueryService.RefundsQueryAsync(request);

                // 使用优化的响应处理
                response.HandleResponse("退款查询", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new RefundsQueryResponse();
                response.HandleResponseWithException("退款查询", config, ex);
            }

            Console.WriteLine("退款查询示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 退款查询示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await RefundsQueryDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 退款查询示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 